﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;

namespace BOH.ControlPanel
{
    public partial class SubjectModify : System.Web.UI.Page
    {
        protected string pageType = string.Empty;
        protected string ID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            pageType = Request.QueryString["type"];
            ID = Request.QueryString["ID"];
            if (!IsPostBack)
            {
                if (pageType != null && ID != null && ID.Trim().Length > 0 && pageType == "edit")
                {
                    getSubjectByID(ID);
                }
            }
        }

        public void getSubjectByID(string ID)
        {
            DataSet dst = Subject.getAllSubject(ID);
            if (dst.Tables[0].Rows.Count > 0)
            {
                txtSubjectID.Text = dst.Tables[0].Rows[0]["SubjectID"].ToString();
                txtSubjectName.Text = dst.Tables[0].Rows[0]["SubjectName"].ToString();
                drpStatus.SelectedValue = dst.Tables[0].Rows[0]["Status"].ToString();
                drpApplyStyle.SelectedValue = dst.Tables[0].Rows[0]["AccreditationLevel"].ToString();
                txtOrderID.Text = dst.Tables[0].Rows[0]["OrderID"].ToString();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            int intOrderID = 0;
            if (!string.IsNullOrEmpty(txtOrderID.Text))
                intOrderID = int.Parse(txtOrderID.Text);
            if (pageType != null && ID != null && ID.Trim().Length > 0 && pageType == "edit")
            {
                Subject.UpdateSubject(ID, txtSubjectID.Text.Trim(), txtSubjectName.Text.Trim(), drpStatus.SelectedValue, drpApplyStyle.SelectedValue, intOrderID);
            }
            else
            {
                Subject.InsertSubject(txtSubjectID.Text.Trim(), txtSubjectName.Text.Trim(), drpStatus.SelectedValue, drpApplyStyle.SelectedValue, intOrderID);
            }

            Response.Redirect("SubjectList.aspx");
        }
    }
}
